// pages/cardrenzheng/cardrenzheng.js
import cache from '../../utils/Cache.js';
import url from '../../utils/url.js';
import http from '../../utils/http.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    upfile:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
      http.http(url.getuserinfo,'GET',{openid:cache.getSorg('openid')},function(res){
        console.log(res.userinfo)
        that.setData({
            userinfo: res.userinfo,
            upfile:res.userinfo.card_img
          });
      });
  },
  upfile:function(){
    var that = this
      wx.chooseImage({
        count:3,
        sizeType: ['original','compressed'],
        sourceType: ['album','camera'],
        success: function(res) {
            //tempFilePath可以作为img标签的src属性显示在页面上
            //第一步获取到图片
            const tempFilePaths = res.tempFilePaths;
            // console.log(tempFilePaths)
            tempFilePaths.map(file => {
              //文件上传
              wx.uploadFile({
                url: url.upfile,
                filePath: file,
                name: 'file',
                formData: {
                  openid: cache.getSorg('openid')
                },
                header: {
                  'content-type': 'application/json', // 默认值
                  'Authorization': cache.getSorg('token')
                },
                success: function (res) {
                  let json =JSON.parse(res.data);
                
                  that.setData({
                    //...参数的展开
                    upfile: [...that.data.upfile, json]
                  })
                }
              })
            });
            
        },
      })
  },
  dopost:function(evt){
    //得到表单传递过来的数据
      let formData = evt.detail.value;
      if(!formData.truename){
          wx.showToast({
            title: '请填写姓名',
            icon:'none'
          })
          return false;
      }
    if (!formData.card){
        wx.showToast({
          title: '请填写身份证号',
          icon:'none'
        })
        return false;
      }
      //得到传递的身份证照片
      let card_img = [];
      this.data.upfile.map(item => {
        card_img.push(item.data)
      });
      if(card_img.length == 0){
          wx.showToast({
            title: '请上传身份照片',
            icon:'none'
          })
          return false;
      }
      formData.card_img = card_img.join('#')
      formData.openid = cache.getSorg('openid')
      console.log(formData)
    http.http(url.updateuserinfo,'POST',formData,function(res){
        wx.showToast({
          title: res.msg
        })
    });
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
})